define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dts/point/time/index' + location.search,
                    add_url: 'dts/point/time/add',
                    edit_url: 'dts/point/time/edit',
                    del_url: 'dts/point/time/del',
                    multi_url: 'dts/point/time/multi',
                    import_url: 'dts/point/time/import',
                    table: 'dts_point_time',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'), searchList: {"not":__('Type not'),"day":__('Type day'),"week":__('Type week'),"toweek":__('Type toweek'),"month":__('Type month')}, formatter: Table.api.formatter.normal},
                        {field: 'is_even', title: __('Is_even'), searchList: {"0":__('Is_even 0'),"1":__('Is_even 1')}, formatter: Table.api.formatter.normal},
                        {field: 'nottime', title: __('Nottime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'endtime', title: __('结束时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.date},
                        {field: 'weekarr', title: __('Weekarr'), operate: false, searchList: {"0":__('Weekarr 0'),"1":__('Weekarr 1'),"2":__('Weekarr 2'),"3":__('Weekarr 3'),"4":__('Weekarr 4'),"5":__('Weekarr 5'),"6":__('Weekarr 6')}, 
                            formatter:function(value, row, index) {
                                var textArr = [];
                                
                                if(value) {
                                    let arr = value.split(',');
                                    arr.forEach(element => {
                                        textArr.push(__('Weekarr '+element));
                                    });
                                }
                                
                                return textArr.join(',');
                            }
                        },
                        {field: 'months', title: __('Months'), operate: false},
                        {field: 'startperiod', title: __('Startperiod')},
                        {field: 'endperiod', title: __('Endperiod')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    // 动态关联查询条件
    $(document).on('input change', '#c-type', function () {
        var type = $('#c-type').val();
        if(type === 'not') {
            $('#iseven-div').hide();
            $('#weekarr-div').hide();
            $('#months-div').hide();
        }
        if(type === 'day') {
            $('#iseven-div').hide();
            $('#weekarr-div').hide();
            $('#months-div').hide();
        }
        if(type === 'week') {
            $('#iseven-div').hide();
            $('#weekarr-div').show();
            $('#months-div').hide();
        }
        if(type === 'toweek') {
            $('#iseven-div').show();
            $('#weekarr-div').show();
            $('#months-div').hide();
        }
        if(type === 'month') {
            $('#iseven-div').hide();
            $('#weekarr-div').hide();
            $('#months-div').show();
        }
    });

    return Controller;
});
